package com.nanhang.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nanhang.common.util.PageResult;
import com.nanhang.entity.Classes;
import com.nanhang.entity.Student;
import com.nanhang.mapper.StudentMapper;
import com.nanhang.service.IClassesService;
import com.nanhang.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nanhang.studentsearch.StudentS0;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ym
 * @since 2025-01-07
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {

    @Autowired
    private IClassesService classesService;

    @Override
    public PageResult<Student>findbypage(Integer page,Integer pagesize){
        Page<Student> stupage = this.page(new Page<>(page,pagesize));
        return new PageResult<>(stupage.getRecords(),stupage.getTotal());
    }

    @Override
    public PageResult<Student> searchSTU(Integer page, Integer pagesize, StudentS0 S0){
        QueryWrapper<Student> stu =new QueryWrapper<>();
        stu.like(StrUtil.isNotBlank(S0.getSname()),"sname",S0.getSname());
        stu.like(StrUtil.isNotBlank(S0.getAddr()),"addr",S0.getAddr());
        stu.eq(S0.getCid() != null && S0.getCid() != 0,"cid",S0.getCid());

        Page<Student> stupage = this.page(new Page<>(page,pagesize),stu);

        stupage.getRecords().forEach(s->{
            Classes classes = classesService.getById(s.getCid());
            s.setCname(classes.getCname());
        });

        return new PageResult<>(stupage.getRecords(),stupage.getTotal());
    }

}
